home *** CD-ROM | disk | FTP | other *** search
/ PC Open 97 / PC Open 97 CD2.bin / Demo / FileMaker / Data1.cab / fmi_iwpsort.js < prev    next >
Encoding:
JavaScript  |  2004-02-10  |  9.2 KB  |  383 lines

  1. /*
  2.     fmi_iwpsort.js
  3.     
  4.     (c) Copyright 2004 FileMaker, Inc. All rights reserved.
  5. */
  6.  
  7. function setupSortDlog()
  8. {
  9.     if( top == null || top.sortinfo == null || top.opener == null || top.opener.iwp == null )
  10.     {
  11.         top.close();
  12.         return;
  13.     }
  14.     
  15.     var iwp = top.opener.iwp;
  16.     var strs = iwp.strings;
  17.     if( strs != null  )
  18.     {
  19.         var d = window.document;
  20.         d.title = strs.sort_ttl_sort;
  21.         iwp.addOrReplaceTextNode( d, "orderBox", strs.sort_lbl_order );
  22.         iwp.addOrReplaceTextNode( d, "chooseBox", strs.sort_lbl_choose );
  23.         iwp.addOrReplaceTextNode( d, "ascendBox1", strs.sort_lbl_ascend1 );
  24.         iwp.addOrReplaceTextNode( d, "ascendBox2", strs.sort_lbl_ascend2 );
  25.         iwp.addOrReplaceTextNode( d, "decendBox1", strs.sort_lbl_decend1 );
  26.         iwp.addOrReplaceTextNode( d, "decendBox2", strs.sort_lbl_decend2 );
  27.         iwp.addOrReplaceTextNode( d, "customBox1", strs.sort_lbl_custom1 );
  28.         iwp.addOrReplaceTextNode( d, "customBox2", strs.sort_lbl_custom2 );
  29.         iwp.setAttribute( d, "clearButton", "value", strs.sort_btn_clearall );
  30.         iwp.setAttribute( d, "moveButton", "value", strs.sort_btn_move );
  31.         iwp.setAttribute( d, "sortButton", "value", strs.sort_btn_sort );
  32.         iwp.setAttribute( d, "unsortButton", "value", strs.sort_btn_unsort );
  33.         iwp.setupRollover( d, "helpButton", "sorthelp" );
  34.         
  35.         var nav = null;
  36.         var sflds = top.sortinfo.fields;
  37.         var svals = top.sortinfo.vlists;
  38.         var curord = top.sortinfo.currentorder;
  39.         var dlg = d.getElementById( "dlogBox" );
  40.         
  41.         dlg.s1active            = null;
  42.         dlg.ascbtn                = d.getElementById( "ascendButton" );
  43.         dlg.clrbtn                = d.getElementById( "clearButton" );
  44.         dlg.custbtn                = d.getElementById( "customButton" );
  45.         dlg.decbtn                = d.getElementById( "decendButton" );
  46.         dlg.hlpbtn                = d.getElementById( "helpButton" );
  47.         dlg.movebtn                = d.getElementById( "moveButton" );
  48.         dlg.s1                    = d.getElementById( "select1" );
  49.         dlg.s2                    = d.getElementById( "select2" );
  50.         dlg.s3                    = d.getElementById( "select3" );
  51.         dlg.srtbtn                = d.getElementById( "sortButton" );
  52.         dlg.usrtbtn                = d.getElementById( "unsortButton" );
  53.         dlg.s1.firstclick        = false;
  54.                     
  55.         dlg.srtbtn.submit = function()
  56.         {
  57.             var url = iwp.cgiPath();
  58.             var src = dlg.s2.options;
  59.             for ( i = 0; i < src.length; ++i )
  60.             {
  61.                 var index = i + 1;
  62.                 url += "-sortfield." + index + "=" + src[i].indx +
  63.                         "&-sortorder." + index + "=" + src[i].value + "&";
  64.             }
  65.             url += "-iwpsort";
  66.             var bFrame = iwp.bodyFrame;
  67.             if( bFrame != null )
  68.                 bFrame.location = url;
  69.             top.close();
  70.         }
  71.         
  72.         dlg.usrtbtn.submit = function()
  73.         {
  74.             iwp.submitAndContinue( [{target: "-submitclose", n: "-unsort", v: ""}] );
  75.             top.close();
  76.         }
  77.         
  78.         dlg.moveitems = function()
  79.         {
  80.             if ( this.s1active == true )
  81.             {
  82.                 var src = this.s1.options;
  83.                 var dest = this.s2.options;
  84.                 var cnt = 0;
  85.                 for ( var i = 0; i < src.length; ++i )
  86.                 {
  87.                     if ( src[i].selected )
  88.                     {
  89.                         if ( src[i].dupcheck(dest) )
  90.                         {
  91.                             src[i].selected = false;
  92.                             var tmp = this.makeoption(src[i].text,src[i].value,src[i].indx);
  93.                             tmp.setinfo();
  94.                             dest[dest.length] = tmp;
  95.                             ++cnt;
  96.                         }
  97.                     }
  98.                 }
  99.                 if ( cnt > 0 )
  100.                 {
  101.                     this.s1.blur();
  102.                     this.s2.handlefocus();
  103.                 }
  104.             }
  105.             else if ( this.s1active == false )
  106.                 this.clear( false );
  107.         }
  108.  
  109.         dlg.s1.unselect = dlg.s2.unselect = function ()
  110.         {
  111.             for ( var i = 0; i < this.options.length; ++i ) this.options[i].selected = false;
  112.         }
  113.         
  114.         dlg.s1.selectcount = dlg.s2.selectcount = function ()
  115.         {
  116.             var cnt = 0;
  117.             for ( var i = 0; i < this.options.length; ++i ) 
  118.             {
  119.                 if ( this.options[i].selected ) ++cnt;
  120.             }
  121.             return cnt;
  122.         }
  123.  
  124.         dlg.clear = function( all )
  125.         {    
  126.             var src = this.s2.options;
  127.             
  128.             if( all )
  129.             {
  130.                 for ( var i = 0; i < src.length; ++i ) src[i--] = null;
  131.             }
  132.             else
  133.             {
  134.                 for ( var i = 0; i < src.length; ++i )
  135.                 {
  136.                     if ( src[i].selected )
  137.                         src[i--] = null;
  138.                 }
  139.             }
  140.             this.s1.unselect();
  141.             this.s2.unselect();
  142.             dlg.s1active = null;
  143.             this.disablesortradios(true);
  144.             this.clearsortbuttons();
  145.             this.s1.firstclick = false;
  146.         }
  147.  
  148.         dlg.s1.handlefocus = dlg.s2.handlefocus = function()
  149.         {
  150.             var olist, txt;
  151.             if ( this.id == dlg.s1.id )
  152.             {
  153.                 dlg.s1active = true;
  154.                 olist = dlg.s2;
  155.                 txt = strs.sort_btn_move;
  156.                 if (this.firstclick == false )
  157.                 {
  158.                     dlg.ascbtn.checked = true;
  159.                     this.firstclick = true;
  160.                 }
  161.             }
  162.             else
  163.             {
  164.                 dlg.s1active = false;
  165.                 olist = dlg.s1;
  166.                 txt = strs.sort_btn_clear;
  167.             }
  168.             this.selections = this.selectcount();
  169.             olist.selections = 0;
  170.             olist.unselect();
  171.             if( this.selections > 0 )
  172.             {
  173.                 dlg.disablesortradios(false);
  174.             }
  175.             dlg.movebtn.value = txt;
  176.             this.focus();
  177.         }
  178.  
  179.         dlg.s2.setsortorder = function()
  180.         {                
  181.             if ( dlg.s1active == false )
  182.             {
  183.                 for ( var i = 0; i < this.options.length; ++i )
  184.                 {
  185.                     if(this.options[i].selected)
  186.                         this.options[i].setinfo();
  187.                 }
  188.             }
  189.             if ( dlg.custbtn.checked && ! dlg.custbtn.disabled )
  190.                 dlg.s3.disabled = false;
  191.             else
  192.                 dlg.s3.disabled = true;
  193.         }
  194.  
  195.         dlg.s2.updatesortstate = function()
  196.         {
  197.             if ( dlg.s1active == false )
  198.             {
  199.                 var s2 = this.options;
  200.                 var sel = new Array();
  201.                 for ( var i = 0, j = 0; i < s2.length; ++i )
  202.                 {
  203.                     if ( s2[i].selected )
  204.                         sel[j++] = s2[i];
  205.                 }
  206.                 if ( sel.length > 0 )
  207.                 {
  208.                     var cval = sel[0].value;
  209.                     for ( var i = 1; i < sel.length; ++i )
  210.                     {
  211.                         if ( sel[i].value != cval )
  212.                         {
  213.                             dlg.clearsortbuttons();
  214.                             return;
  215.                         }
  216.                     }
  217.                     sel[sel.length - 1].updatesortbuttons();
  218.                 }
  219.             }
  220.         }
  221.         
  222.         dlg.clearsortbuttons = function()
  223.         {
  224.             this.ascbtn.checked = false;
  225.             this.decbtn.checked = false;
  226.             this.custbtn.checked = false;
  227.             this.s3.disabled = true;
  228.         }
  229.  
  230.         dlg.disablesortradios = function(dis)
  231.         {
  232.             if ( dis != null )
  233.             {
  234.                 this.ascbtn.disabled = dis;
  235.                 this.decbtn.disabled = dis;
  236.                 this.custbtn.disabled = dis;
  237.                 if( ! dis && this.custbtn.checked  )
  238.                     this.s3.disabled = false;
  239.                 else
  240.                     this.s3.disabled = true;
  241.                 this.updatebuttons();
  242.             }
  243.         }
  244.         
  245.         dlg.updatebuttons = function()
  246.         {
  247.             var s1len = this.s1.options.length;
  248.             var s2len = this.s2.options.length;
  249.             
  250.             if ( s1len > 0 && this.s1active != null && ( this.s1.selections > 0 || this.s2.selections > 0) )
  251.                 this.movebtn.disabled = false;
  252.             else
  253.                 this.movebtn.disabled = true;
  254.             if ( s2len > 0 )
  255.             {
  256.                 this.clrbtn.disabled = false;
  257.                 this.srtbtn.disabled = false;
  258.             }
  259.             else
  260.             {
  261.                 this.clrbtn.disabled = true;
  262.                 this.srtbtn.disabled = true;
  263.             }
  264.         }
  265.         
  266.         dlg.makeoption = function(txt,val,idx)
  267.         {
  268.             var op = new Option();
  269.             op.value = val;
  270.             op.text = txt;
  271.             op.rtext = txt;
  272.             op.indx = idx;
  273.  
  274.             op.setinfo = function()
  275.             {
  276.                 var sortstring;
  277.                 if ( dlg.decbtn.checked )
  278.                 {
  279.                     this.value = dlg.decbtn.value;
  280.                     sortstring = strs.sort_lbl_decend1;
  281.                 }
  282.                 else if ( dlg.custbtn.checked )
  283.                 {
  284.                     var vals = dlg.s3.options;
  285.                     for ( var i = 0; i < vals.length; ++i )
  286.                     {
  287.                         if ( vals[i].selected )
  288.                         {
  289.                             this.value = vals[i].indx;
  290.                             break;
  291.                         }
  292.                     }
  293.                     sortstring = strs.sort_lbl_custom1;
  294.                 }
  295.                 else
  296.                 {
  297.                     this.value = dlg.ascbtn.value;
  298.                     sortstring = strs.sort_lbl_ascend1;
  299.                 }
  300.                 this.text = sortstring + " " + this.rtext;
  301.                 this.selected = true;
  302.             }
  303.  
  304.             op.initinfo = function()
  305.             {
  306.                 var sortstring;
  307.                 if ( this.value == dlg.ascbtn.value )
  308.                     sortstring = strs.sort_lbl_ascend1;
  309.                 else if ( this.value == dlg.decbtn.value )
  310.                     sortstring = strs.sort_lbl_decend1;
  311.                 else
  312.                     sortstring = strs.sort_lbl_custom1;
  313.                 this.text = sortstring + " " + this.rtext;
  314.             }
  315.  
  316.         //    alert user that a duplicate field is in a sort list
  317.             op.dupcheck = function(dest)
  318.             {
  319.                 var result = true;
  320.                 for ( var i = 0; i < dest.length; ++i )
  321.                 {
  322.                     if ( this.text == dest[i].rtext )
  323.                     {
  324.                         alert(strs.sort_err_dup);
  325.                         result = false;
  326.                         break;
  327.                     }
  328.                 }
  329.                 return result;
  330.             }
  331.  
  332.             op.updatesortbuttons = function()
  333.             {
  334.                 if ( this.value == dlg.ascbtn.value )
  335.                     dlg.ascbtn.checked = true;
  336.                 else if ( this.value == dlg.decbtn.value )
  337.                     dlg.decbtn.checked = true;
  338.                 else
  339.                 {
  340.                     dlg.custbtn.checked = true;
  341.                     var opts = dlg.s3.options;
  342.                     for( var i = 0; i < opts.length; ++i )
  343.                     {
  344.                         if( opts[i].indx == this.value )
  345.                         {
  346.                             opts[i].selected = true;
  347.                             break;
  348.                         }
  349.                     }
  350.                 }
  351.             }
  352.  
  353.             return op;
  354.         }
  355.         
  356.         dlg.hlpbtn.showhelp = function()
  357.         {
  358.             iwp.openHelpWindow( "Sorting_records" );
  359.         }
  360.         
  361.         dlg.disablesortradios(true);
  362.         for ( var i in sflds )
  363.             dlg.s1.options[i] = dlg.makeoption(sflds[i].name,sflds[i].name,sflds[i].index);
  364.         if ( curord.length > 0 )
  365.         {
  366.             var dest = dlg.s2.options;
  367.             for ( var i in curord )
  368.             {
  369.                 var tmp = dlg.makeoption(curord[i].name,curord[i].order,curord[i].index);
  370.                 tmp.initinfo();
  371.                 dest[i] = tmp;
  372.             }
  373.             dlg.updatebuttons();
  374.         }
  375.         if ( svals.length == 0 )
  376.             dlg.s3.options[0] = new Option(strs.sort_lbl_nolists);
  377.         else
  378.         {
  379.             for ( var i in svals )
  380.                 dlg.s3.options[i] = dlg.makeoption(svals[i].name,svals[i].name,svals[i].index);
  381.         }
  382.     }
  383. }